//
//  ImportPageViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/27.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "ImportPageViewController.h"

@interface ImportPageViewController ()

@end

@implementation ImportPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initAdBook];
    

    //[self readContacts];
    // Do any additional setup after loading the view from its nib.
}
//***重新通讯录
//初始化通讯录
-(void)initAdBook
{
    ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc] init];
    //UINavigationController *mainNav=[UINavigationController alloc]initWithRootViewController:nav];

    nav.peoplePickerDelegate = self;
    
    double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。
    if(version>8.0f){
        nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
    }
    [self presentViewController:nav animated:YES completion:nil];
}
//取消选择
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
//IOS8
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    
    NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSString *middleName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
    
    if (firstName==NULL){firstName=@"";}
    if (lastName==NULL){lastName=@"";}
    if (middleName==NULL){middleName=@"";}
    
    NSString *fullName=[lastName stringByAppendingString:middleName];
    fullName=[fullName stringByAppendingString:firstName];
    
    ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
    long index = ABMultiValueGetIndexForIdentifier(phone,identifier);
    NSString *phoneNO = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, index);
    
    if ([phoneNO hasPrefix:@"+"]) {
        phoneNO = [phoneNO substringFromIndex:3];
    }
    
    phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""];
    NSLog(@"%@", fullName);
    NSLog(@"%@", phoneNO);
    
    
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return;


}

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
    ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
    personViewController.displayedPerson = person;
    [peoplePicker pushViewController:personViewController animated:YES];
}
//IOS 7

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    return YES;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSString *middlename = (__bridge NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
    
    NSString *fullName=[lastName stringByAppendingString:middlename];
    fullName=[fullName stringByAppendingString:firstName];
    
    ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
    long index = ABMultiValueGetIndexForIdentifier(phone,identifier);
    NSString *phoneNO = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, index);
    
    if ([phoneNO hasPrefix:@"+"]) {
        phoneNO = [phoneNO substringFromIndex:3];
    }
    
    phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""];
    NSLog(@"%@", fullName);
    NSLog(@"%@", phoneNO);
    
    
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];

    return YES;
}


//***旭的读取通讯录代码
- (void) readContacts {
    ABAddressBookRef tmpAddressBook = nil;
    if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
        tmpAddressBook=ABAddressBookCreateWithOptions(NULL, NULL);
        dispatch_semaphore_t sema=dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(tmpAddressBook, ^(bool greanted, CFErrorRef error){
            dispatch_semaphore_signal(sema);
        });
        
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }
    else
    {
        tmpAddressBook = ABAddressBookCreate();
    }
    
    if (tmpAddressBook==nil) {
        return ;
    };
    NSArray* tmpPeoples = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
    
    for(int i = 0 ; i < [tmpPeoples count] ; i++)
        
    {
        ABRecordRef tmpPerson = (__bridge ABRecordRef)[tmpPeoples objectAtIndex:i];
        if(ABPersonHasImageData(tmpPerson))
        {
            //NSData * imageData = ( NSData *)ABPersonCopyImageData(record);
            NSData * imageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(tmpAddressBook,kABPersonImageFormatThumbnail);
            UIImage * image = [[UIImage alloc] initWithData:imageData];
            
        }
        
        //获取的联系人单一属性:First name
        
        NSString* tmpFirstName = (__bridge NSString*)ABRecordCopyValue(tmpPerson, kABPersonFirstNameProperty);
        
        NSLog(@"First name:%@", tmpFirstName);
        
        
        //获取的联系人单一属性:Last name
        
        NSString* tmpLastName = (__bridge NSString*)ABRecordCopyValue(tmpPerson, kABPersonLastNameProperty);
        
        NSLog(@"Last name:%@", tmpLastName);
        
        
        //获取的联系人单一属性:Nickname
        
        NSString* tmpNickname = (__bridge NSString*)ABRecordCopyValue(tmpPerson, kABPersonNicknameProperty);
        
        NSLog(@"Nickname:%@", tmpNickname);
        
        
        //获取的联系人单一属性:Company name
        
        NSString* tmpCompanyname = (__bridge NSString*)ABRecordCopyValue(tmpPerson, kABPersonOrganizationProperty);
        
        NSLog(@"Company name:%@", tmpCompanyname);
        
        //获取的联系人单一属性:Job Title
        
        NSString* tmpJobTitle= (__bridge NSString*)ABRecordCopyValue(tmpPerson, kABPersonJobTitleProperty);
        
        NSLog(@"Job Title:%@", tmpJobTitle);
        
        //获取的联系人单一属性:Department name
        
        NSString* tmpDepartmentName = (__bridge NSString*)ABRecordCopyValue(tmpPerson, kABPersonDepartmentProperty);
        
        NSLog(@"Department name:%@", tmpDepartmentName);
        
        
        //获取的联系人单一属性:Email(s)
        
        ABMultiValueRef tmpEmails = ABRecordCopyValue(tmpPerson, kABPersonEmailProperty);
        
        for(NSInteger j = 0; j < ABMultiValueGetCount(tmpEmails); j++)
            
        {
            
            NSString* tmpEmailIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpEmails, j);
            
            NSLog(@"Emails%d:%@", j, tmpEmailIndex);
            
        }
        
        CFRelease(tmpEmails);
        
        //获取的联系人单一属性:Birthday
        
        NSDate* tmpBirthday = (__bridge NSDate*)ABRecordCopyValue(tmpPerson, kABPersonBirthdayProperty);
        
        NSLog(@"Birthday:%@", tmpBirthday);
        
        //获取的联系人单一属性:Note
        
        NSString* tmpNote = (__bridge NSString*)ABRecordCopyValue(tmpPerson, kABPersonNoteProperty);
        
        NSLog(@"Note:%@", tmpNote);
        
        //获取的联系人单一属性:Generic phone number
        
        ABMultiValueRef tmpPhones = ABRecordCopyValue(tmpPerson, kABPersonPhoneProperty);
        
        for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++)
            
        {
            
            NSString* tmpPhoneIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);
            
            NSLog(@"tmpPhoneIndex%d:%@", j, tmpPhoneIndex);
            
        }
        
        CFRelease(tmpPhones);
        
    }
    
    //释放内存
    
    //[tmpPeoples release];
    
    CFRelease(tmpAddressBook);
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
